Can the display()
method see the object's instance variables
(such as balance
)?
Yes.
Statements of a method (such as display()
) can see
the object's instance variables and
the object's other methods.
They cannot see the parameters (and local variables) of other methods.
Here is another look at the CheckingAccount
class:
class CheckingAccount { . . . . private int balance; . . . . void processDeposit( int amount ) { // scope of amount starts here balance = balance + amount ; // scope of amount ends here } void processCheck( int amount ) { // scope of amount starts here int charge; incrementUse(); if ( balance < 100000 ) charge = 15; else charge = 0; balance = balance - amount - charge ; // scope of amount ends here } }
Two methods are using the same identifier, amount
,
for two different formal parameters.
Each method has its own formal parameter
completely separate from the other method.
This is OK.
The scopes of the two parameters do not overlap so the statements in one method
cannot "see" the formal parameter of the other method.
For example the statement
balance = balance - amount - charge ;
from the second method can only see the formal parameter of that method---the scope of the formal parameter of the other method does not include this statement.